package com.yuanjin.Fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Fragment;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.yuanjin.R;
import com.yuanjin.adapter.ChatText_Adapter;
import com.yuanjin.util.HttpData;
import com.yuanjin.util.HttpGetDataListener;
import com.yuanjin.util.ListData_tulingChat;

/**
 * @author Administrator ʵ�ֽӿ� HttpGetDataListener����д�ӿ��еĳ��󷽷�getDataUrl()
 */
public class Fragment_TulingChating2 extends Fragment implements
		HttpGetDataListener, OnClickListener {
	private ListView mListView;
	private Button btn_send;
	private EditText editText;
	private String content_str;
	private HttpData httpData;
	private double currenttime, oldtime = 0;
	private List<ListData_tulingChat> lists;
	private ChatText_Adapter adapter;
	private String[] welcome_array;

	// protected void onCreate(Bundle savedInstanceState) {
	// // TODO Auto-generated method stub
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.fragment_chat_tuling);
	// initview();
	// }
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater
				.inflate(R.layout.fragment_chat_tuling, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mListView = (ListView) getView().findViewById(R.id.lv_tulingchat);
		btn_send = (Button) getView().findViewById(R.id.send_btn);
		btn_send.setOnClickListener(this);
		editText = (EditText) getView().findViewById(R.id.sendText);
		lists = new ArrayList<ListData_tulingChat>();
		adapter = new ChatText_Adapter(lists, getActivity());
		mListView.setAdapter(adapter);
		// ��ʼ����ӭ��
		ListData_tulingChat listdata;
		listdata = new ListData_tulingChat(getRandomWelcomeTips(),
				ListData_tulingChat.RECEIVER, getTime());
		lists.add(listdata);
	}

	// private void initview() {
	// mListView = (ListView) findViewById(R.id.lv_tulingchat);
	// btn_send = (Button) findViewById(R.id.send_btn);
	// btn_send.setOnClickListener(this);
	// editText = (EditText) findViewById(R.id.sendText);
	// lists = new ArrayList<ListData_tulingChat>();
	// adapter = new ChatText_Adapter(lists, this);
	// mListView.setAdapter(adapter);
	// // ��ʼ����ӭ��
	// ListData_tulingChat listdata;
	// listdata = new ListData_tulingChat(getRandomWelcomeTips(),
	// ListData_tulingChat.RECEIVER, getTime());
	// lists.add(listdata);
	// }

	// ��ȡ�����ӭ��
	public String getRandomWelcomeTips() {
		String welcome_tips = null;
		// ��ȡ�Ѿ�����õĻ�ӭ��������Դ
		welcome_array = getResources().getStringArray(R.array.welcome_tips);
		// ����һ�������
		int index = (int) (Math.random() * (welcome_array.length - 1));
		welcome_tips = welcome_array[index];
		return welcome_tips;
	}

	@Override
	public void onClick(View v) {
		content_str = editText.getText().toString();
		// ȥ���ո�
		content_str = content_str.replace(" ", "");
		// ȥ���س�
		content_str = content_str.replace("\n", "");
		ListData_tulingChat listdata;
		listdata = new ListData_tulingChat(content_str,
				ListData_tulingChat.SEND, getTime());
		lists.add(listdata);
		// �����¼��Ŀ����30��ɾ����¼��ǰʮ��
		if (lists.size() > 30) {
			for (int i = 0; i < 10; i++) {
				lists.remove(0);
			}
		}
		adapter.notifyDataSetChanged();
		// ʵ����httpdata
		httpData = (HttpData) new HttpData(
				"http://www.tuling123.com/openapi/api?key=3413a7bda7eaee872e787520400e42db&info="
						+ content_str, this).execute();
		// ���Ϳ�����
		editText.setText(null);
	}

	@Override
	public void getDataUrl(String url) {
		// ����ֱ�����data �Բ鿴data���ݵ�����
		// Log.e("test", url);
		// ���ý���������������
		parseText(url);
	}

	// �������յ���JSON��ʽ������
	public void parseText(String str) {
		try {
			JSONObject jb = new JSONObject(str);
			// ��������������������ListData��װ����
			ListData_tulingChat listdata = new ListData_tulingChat(
					jb.getString("text"), ListData_tulingChat.RECEIVER,
					getTime());
			lists.add(listdata);
			// �����µ����ݲ����󣬶����ݽ�����������
			adapter.notifyDataSetChanged();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getTime() {
		currenttime = System.currentTimeMillis();
		// ��ȡ���ӻ�ʱ������
		String dateTime = DateFormat.format("yyyy.MM.dd hh.mm",
				System.currentTimeMillis()).toString();
		// ÿ��5������ʾһ��ʱ��
		if (currenttime - oldtime >= 5 * 60 * 1000) {
			oldtime = currenttime;
			return dateTime;
		} else {
			return "";
		}
	}

}
